package com.ruoyi.central.enums;

import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.ruoyi.common.config.jackson.IEnumDeserializer;
import com.ruoyi.common.utils.CollectionUtils;
import lombok.Getter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Classname MaterialStatusEnum
 * @Author: sunluquan
 * @Date: 2022/5/9 14:10
 * @Description TODO
 * 物料状态枚举
 */
@Getter
@JsonDeserialize(using = IEnumDeserializer.class)
public enum MaterialStatusEnum  implements IEnum<Integer> {
    FREE_OF_STATUS(1,"未使用"),
    USE_OF_STATUS(2,"使用中"),
    FINISHED_OF_STATUS(3,"已使用"),
    //Combined 
    COMBINED_OF_STATUS(4,"已合并");

    /** k前状态可过渡的状态*/
    private final static Map<MaterialStatusEnum, MaterialStatusEnum> TRANSITION_MAP;
    /** unmodifiable */
    private final static Set<MaterialStatusEnum> UNMODIFIABLE_SET;
    static{
        TRANSITION_MAP = new HashMap<>();
        TRANSITION_MAP.put(FREE_OF_STATUS, USE_OF_STATUS);
        TRANSITION_MAP.put(USE_OF_STATUS, FINISHED_OF_STATUS);
        TRANSITION_MAP.put(FINISHED_OF_STATUS, COMBINED_OF_STATUS);
        TRANSITION_MAP.put(COMBINED_OF_STATUS, null);

        UNMODIFIABLE_SET = Stream.of(USE_OF_STATUS,FINISHED_OF_STATUS,COMBINED_OF_STATUS).collect(Collectors.toSet());

    }
    
    @JsonValue
    private int value;
    private String description;

    MaterialStatusEnum(int value, String description) {
        this.value = value;
        this.description = description;
    }
    @Override
    public Integer getValue() {
        return value;
    }
    /**
     * 判断当前状态是否可以过渡到下一个状态
     * @param target 目标状态
     * @return true可以过渡到目标状态 false 不能够过渡到目标状态
     */
    public boolean isTransition(MaterialStatusEnum target){
        MaterialStatusEnum source = this ;
        return TRANSITION_MAP.containsKey(source) && TRANSITION_MAP.get(source)  == target;
    }

    /**
     * 获取 不能修改的状态 列表
     * @return
     */
    public static Set<MaterialStatusEnum> getUnmodifiableSet(){
        return UNMODIFIABLE_SET;
    }
}
